package com.gdssoft.pdaweb.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.gdssoft.core.command.QueryFilter;
import com.gdssoft.core.service.impl.BaseServiceImpl;
import com.gdssoft.core.util.BeanUtil;
import com.gdssoft.core.util.ContextUtil;
import com.gdssoft.core.util.DateUtil;
import com.gdssoft.framework.model.system.Department;
import com.gdssoft.framework.service.system.DepartmentService;
import com.gdssoft.pdaweb.dao.PdaMdAssetInfoDao;
import com.gdssoft.pdaweb.model.PdaMdAssetInfo;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;
import com.gdssoft.pdaweb.model.water.PdaWaterMeter;
import com.gdssoft.pdaweb.service.CrmService;
import com.gdssoft.pdaweb.service.PdaMdAssetInfoService;
import com.gdssoft.pdaweb.service.am.PdaAmMeterService;
import com.gdssoft.pdaweb.service.water.PdaWaterMeterService;


public class PdaMdAssetInfoServiceImpl extends BaseServiceImpl<PdaMdAssetInfo> implements PdaMdAssetInfoService {
	private PdaMdAssetInfoDao dao;
	public PdaMdAssetInfoServiceImpl(PdaMdAssetInfoDao dao) {
		super(dao);
		this.dao=dao;
	}
	@Resource
	private PdaAmMeterService pdaAmMeterService;
	@Resource
	private PdaWaterMeterService PdaWaterMeterService;
	@Resource
	private CrmService crmService;
	@Resource
	private DepartmentService departmentService;

	@Override
	public void saveSupply(int meterType, PdaAmMeter am, PdaMdAssetInfo asset) throws Exception {
		am.setCreateddt(DateUtil.dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));
		am.setCreatedby(ContextUtil.getCurrentUser().getUsername());
		am.setToCrmFlag(0);
		am.setIsAlert("0");
		am.setIsPlanned("N");
		
		asset = dao.getByBarCode(asset.getBarCode());
		QueryFilter qf = new QueryFilter(0,2);
		qf.addFilter("Q_depCode_S_EQ", asset.getSiteId());
		List<Department> list = departmentService.getAll(qf);
		if(list.size()>0){
			am.setDepartment(list.get(0));
		}else{	//siteId还没有对应的部门，需要在管理处管理中新增depCode=siteId的管理处
			throw new Exception("--没有找到编号为"+asset.getSiteId()+"的管理处");
		}
		
		am.setPdaMdAssetInfo(asset);
		am.setPosition(asset.getLocationId());
		try {
			switch (meterType) {
			case 2: // 电
				if(crmService.pushAmMeter(am)){
					am.setToCrmFlag(8);
				}else{
					am.setToCrmFlag(9);
				}
				pdaAmMeterService.save(am);
				break;
			case 1: // 水
				PdaWaterMeter pdaWaterMeter = new PdaWaterMeter();	
				BeanUtil.copyNotNullProperties(pdaWaterMeter, am);//注意：需要PdaWaterMeter与PdaAmMeter的关键字属性名一致
				if(crmService.pushWaterMeter(pdaWaterMeter)){
					pdaWaterMeter.setToCrmFlag(8);
				}else{
					pdaWaterMeter.setToCrmFlag(9);
				}
				PdaWaterMeterService.save(pdaWaterMeter);
				
				break;
			default:
				throw new Exception("--不确定的资产类型");
			}
		} catch (Exception e) {
			throw new Exception("--保存数据或推送CRM失败");
		}
		try {
			asset.setIsSupply("Y");
			this.save(asset);	//标为已补充资产
		} catch (Exception e) {
			throw new Exception("--修改资产状态失败");
		}
	}

	@Override
	public boolean isExist(int meterType, String amId) {
		boolean isExist = false;
	/*	switch (meterType) {
		case 1: // 电
			isExist = pdaAmMeterService.isExist(amId);
			break;
		case 2: // 水
			isExist = PdaWaterMeterService.isExist(amId);
			break;
		default:
			isExist = false;
			break;
		}*/
		isExist=pdaAmMeterService.isExist(amId);
		if(!isExist){
			isExist = PdaWaterMeterService.isExist(amId);
		}
		return isExist;
	}
	
	@Override
	public boolean isExist(String crmMeterNo) {
		return pdaAmMeterService.isCrmMeterNoExist(crmMeterNo)&& PdaWaterMeterService.isCrmMeterNoExist(crmMeterNo);
	}
}
